//index.js
// 用户，商家，菜单，订单，子订单，支付，搜索，评价
var app = getApp()
var util = require('../../utils/util.js');
var foodcart = require('../../utils/foodcart.js')
Page({
  data: {
    // index
    userInfo: {},

    // order
    orderOk: false,
    // me
    img: '',
    orders: [],
    pagenum: 0,
    pagesize: 10,
    hasnextpage: false,

    currentTab: -1,
    orderstate: '-1',

    oid: 0,
    amount: 0,
  },
  onLoad: function() {
    
  },
  onShow: function() {
    foodcart.getFoodlist(); 
    wx.showTabBar()
    this.setData({ orders: [], pagenum: 0})
    this.setOrder();
    var redpackage = wx.getStorageSync("redpackage");
    var oid=this.data.oid;
    if (redpackage && redpackage.id && oid > 0) {
      var dis = 0;
      var amount = this.data.amount
      if (redpackage.redtype == 0) {
        var discount = redpackage.discount;
        if (discount && discount != 0) {
          dis = (100 - discount) / 100 * amount;
        }
      } else if (redpackage.redtype == 1) {
        var reduction = redpackage.reduction;
        reduction.sort(function(a, b) {
          return b.full - a.full;
        })
        for (var i = reduction.length - 1; i >= 0; i--) {
          if (amount >= reduction[i].full * 100) {
            dis = reduction[i].reduce * 100;
            continue;
          }
        }

      }
      dis = dis.toFixed(0);
      var orders = this.data.orders;
      var redpak = {
        id: redpackage.id,
        disamount: dis,
        title: redpackage.title
      };

      for (var i = 0; i < orders.length; i++) {
        if (orders[i].state == 0 && orders[i].id == oid) {
          orders[i].redpackage = redpak;
          orders[i].disamount = orders[i].redpackage.disamount;
          orders[i].payamount = orders[i].amount - orders[i].redpackage.disamount
        }
      }
      this.setData({ orders: orders})
    }

  },
  setOrder: function(options) {

    var that = this
    var url = util.url.order_list;
   // if (options.pagenum){
    url+="?pagenum="+that.data.pagenum
    //}
    util.fetchGet( 
      url,
      function(ret) {
        var list = ret.data.content;
        if (ret.data.last ) {
          that.setData({ hasnextpage: false })
        }else{
          that.setData({ hasnextpage: true })
        }
        if (list.length > 0) {
          for (var i = 0; i < list.length; i++) {
            var fetchTime = list[i].fetchTime;
            list[i].fetchTime = fetchTime.substring(5, fetchTime.length);
            if (list[i].state == 0) {
              // if (list[i].redpackage){
              //   list[i].payamount = list[i].amount - list[i].redpackage.disamount
              //}else{ 
              list[i].payamount = list[i].amount;
              list[i].disamount = 0;
              // }              
            }
          }
          var orders=that.data.orders;
          orders=orders.concat(list);
          that.setData({
            orders: orders
          })
        }
      }
    )
  },
  // order
  orderOk: function() {
    this.setData({
      orderOk: true
    })
  },
  okCancel: function() {
    this.setData({
      orderOk: false
    })
  },
  calling: function() {
    wx.makePhoneCall({
      phoneNumber: '18316588252',
      success: function() {
        console.log("拨打电话成功！")
      },
      fail: function() {
        console.log("拨打电话失败！")
      }
    })
  },
  okOk: function() {
    this.setData({
      orderOk: false
    })
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  payorder: function(e) {
    var oid = e.currentTarget.dataset.id;
 
    wx.navigateTo({
      url: '/pages/orderDetail/orderDetail?oid=' + oid,
    })

  },
  anotherOrder: function(e) {
    /*var that = this;
    var url = util.url.order_copy;
    url += "?id=" + e.currentTarget.dataset.id;
    util.fetchGet(
      url,
      function(data) {
        var suborders = data.data;
        var carts = [];
        var tnum = 0;
        for (var i = 0; i < suborders.length; i++) {

          suborders[i].selected = true;
          carts.push(suborders[i]);

          tnum += suborders[i].num;

          foodcart.addFoodCart(suborders[i].id, suborders[i].num)
        }*/
        

        wx.navigateTo({
          url: '/pages/submitOrder/submitOrder?oid=' + e.currentTarget.dataset.id,
        })
      
    
  },
  testSubmitOrder: function(data) {
    var url = 'pay/testnotify';
    url += "?out_trade_no=" + data.outTradeNo
    util.fetchGet(
      url,
      function(ret) {
        console.log(ret);
      }
    )
  },
  swichNav: function(e) {
    console.log(e);
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current,
        orderstate: e.target.dataset.current
      })
    }
  },
  toChoosePackage: function(e) {
    var oid = e.currentTarget.dataset.id;
    var amount = e.currentTarget.dataset.amount;
    this.setData({
      oid: oid,
      amount: amount
    })
    wx.navigateTo({
      url: '../myPackage/myPackage?amount=' + amount + "&oid=" + oid,
    })
  },
  onReachBottom:function(){

    if (!this.data.hasnextpage){
      return ;
    }else{
      var pagenum=this.data.pagenum;
      pagenum+=1;
      this.setData({ pagenum: pagenum})
      this.setOrder();
    } 
  },
  getMoreOrder:function(){
    if (!this.data.hasnextpage) {
      return;
    } else {
      var pagenum = this.data.pagenum;
      pagenum += 1;
      this.setData({ pagenum: pagenum })
      this.setOrder();
    } 
  },
  toDetail:function(e){
    var oid=e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/orderDetail/orderDetail?oid='+oid,
    }) 
  },
  goToEvaluate: function (e) {
    var oid = e.currentTarget.dataset.oid;
    var orders=this.data.orders;
    var name ="";
    for(var i=0;i<orders.length;i++){
      if (oid==orders[i].id){
        for (var j = 0; j < orders[i].subOrder.length;j++){
          var suborder = orders[i].subOrder;
          name += suborder[j].name +"+";
        }
      }
    }
    if (name.length>1){
      name=name.substring(0,name.length-1);
    }
    wx.navigateTo({
      url: '../evaluate/evaluate?oid=' + oid +"&name="+name,
    }) 
  }
})